package com.act;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.Enum.InfoType;
import com.easemob.chat.EMChatManager;
import com.easemob.chat.EMConversation;
import com.easemob.chat.EMMessage;
import com.easemob.chat.TextMessageBody;
import com.easemob.exceptions.EaseMobException;
import com.easemob.util.DateUtils;
import com.xike.kyt.R;
import com.xike.util.MyUtil;
import com.xike.util.StringUtil;

public class MessageDetailAct extends BaseAct {
	String strSender;
	List<EMMessage> messages = new ArrayList<EMMessage>();
	ListView listView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.act_message_detail);
		Bundle bundle = getIntent().getExtras();
		if (bundle != null && bundle.containsKey("sender")) {
			strSender = bundle.getString("sender");
			EMConversation conversation = EMChatManager.getInstance().getConversation(strSender);
			conversation.resetUnsetMsgCount();
			messages.addAll(conversation.getAllMessages());
		}

		if (bundle != null && bundle.containsKey("userName")) {
			((TextView) findViewById(R.id.headTitle_tvTitle)).setText(bundle.getString("senderName"));
		}
		initViews();

	}

	@Override
	protected void initViews() {
		findViewById(R.id.headTitle_tv_back).setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
			}
		});

		listView = (ListView) findViewById(R.id.messageDetail_listView);
		MyAdater adapter = new MyAdater();
		listView.setAdapter(adapter);
		listView.setSelection(adapter.getCount() - 1);

		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				EMMessage message = messages.get(position);
				try {
					int infoType = message.getIntAttribute("info_type");
					long info_id = message.getIntAttribute("info_id");
					Log.e("", "infoType:"+infoType);
					Log.e("", "info_id:"+info_id);
					if (infoType == InfoType.Question.getIntValue()) {
						Intent it_question = new Intent(MessageDetailAct.this, Question_DetailAct.class);
						it_question.putExtra("questionId", info_id);
						startActivity(it_question);
					} else if (infoType== InfoType.Answer.getIntValue()) {
						Intent it_answer = new Intent(MessageDetailAct.this, Answer_DetailAct.class);
						it_answer.putExtra("answerId",info_id);
						startActivity(it_answer);
					}
				} catch (EaseMobException e) {
					e.printStackTrace();
				}

			}

		});
	}

	@Override
	protected void updateViews() {

	}

	private class MyAdater extends BaseAdapter {

		@Override
		public int getCount() {
			return messages.size();
		}

		@Override
		public EMMessage getItem(int position) {
			return messages.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder;
			if (convertView == null) {
				convertView = View.inflate(MessageDetailAct.this, R.layout.listitem_message_detail, null);
				holder = new ViewHolder();
				holder.tv_desc = (TextView) convertView.findViewById(R.id.lvitem_messageDetail_tv_desc);
				holder.tv_title = (TextView) convertView.findViewById(R.id.lvitem_messageDetail_tv_title);
				holder.tv_time = (TextView) convertView.findViewById(R.id.lvitem_messageDetail_tv_time);
				holder.tv_content = (TextView) convertView.findViewById(R.id.lvitem_messageDetail_tv_content);
				holder.cutline = (View) convertView.findViewById(R.id.lvitem_messageDetail_v_cutline);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}

			EMMessage message = getItem(position);
			TextMessageBody body = (TextMessageBody) message.getBody();
			// holder.message.setText(SmileUtils.getSmiledText(getContext(), getMessageDigest(lastMessage, (this.getContext()))),
			// BufferType.SPANNABLE);//含表情
			String strDesc = body.getMessage(), strTitle = "", strContent = "";
			try {
				strDesc = message.getStringAttribute("description");
			} catch (EaseMobException e) {
				Log.e("MessageDetailAct", "description is not found");
			}
			try {
				strTitle = message.getStringAttribute("title");
			} catch (EaseMobException e) {
				Log.e("MessageDetailAct", "title is not found");

			}
			try {
				strContent = message.getStringAttribute("content");
			} catch (EaseMobException e) {
				Log.e("MessageDetailAct", "content is not found");
			}
			holder.tv_time.setText(DateUtils.getTimestampString(new Date(message.getMsgTime())));

			MyUtil.setTextViewText(holder.tv_desc, strDesc, 0, "消息");
			MyUtil.setTextViewText(holder.tv_title, strTitle, View.GONE, "");
			MyUtil.setTextViewText(holder.tv_content, strContent, View.GONE, "");
			if (!StringUtil.isNotNull(strTitle) && !StringUtil.isNotNull(strContent)) {
				holder.cutline.setVisibility(View.GONE);
			} else {
				holder.cutline.setVisibility(View.VISIBLE);
			}

			return convertView;
		}

		class ViewHolder {
			TextView tv_desc, tv_title, tv_time, tv_content;
			View cutline;
		}

	}

}
